﻿using System;
using System.Collections.Generic;

using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using FastReport;
using FastReportPractice.Models;
using FastReportPractice.Utilities;


namespace FastReportPractice
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }


        /// <summary>
        /// SingleCollection report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            List<Manufacturer> manus = GenFu.GenFu.ListOf<Manufacturer>();
            Report report = new Report();
            report.Load(@"Templates\Single.frx");
            DataTable dt = TypeConverters.ConvertToDataTable<Manufacturer>(manus);
            dt.TableName = "Table1";
            DataSet set = new DataSet();
            set.Tables.Add(dt);
            report.RegisterData(set);
            report.Show();
        }

        /// <summary>
        /// Hierarchical data report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            List<Manufacturer> manus = GenFu.GenFu.ListOf<Manufacturer>(5);
            foreach(var item in manus)
            {
                List<Product> pro = GenFu.GenFu.ListOf<Product>(5);
                foreach(var itemb in pro)
                {
                    itemb.ProductID = item.ProductID;
                }
                item.Products.AddRange(pro);
            }
            Report report = new Report();
            report.Load(@"Templates\Master-Detail.frx");
            report.RegisterData(manus, "Manufacturer");
            report.Show();
            report.Dispose();
        }

        /// <summary>
        /// ECG Report,a test of multi-reportobject
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ElectricReport eleReport = GenFu.GenFu.New<ElectricReport>();
            //Image img = Image.FromFile(@"Resources\res.png");
            //eleReport.DiagnosisGraph = TypeConverters.ImageToBytes(img);
            ////Test TextWrap！
            //eleReport.ECGExceptedEvent = "安装Streamlink-Twitch-Gui,安装成功，打开程序，会提示进行配置（Streamlink-Twitch-Gui的运行依赖于Streamlink,需要知道StremLink文件的位置)，需要输入Streamlink相关文件的位置，其中：";
            //eleReport.Check_Year = "2017";
            //eleReport.Check_Mouth = "12";
            //eleReport.Check_Day = "12";
            Report report = new Report();
            report.Load(@"Templates\ECGReport_New.frx");
            report.RegisterData(new List<ElectricReport> { eleReport }, "Reports");
            report.Show();
            report.Dispose();
        }

        /// <summary>
        /// chart report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            var context = DBProvider.DefaultDBContext;
            DataTable dtStudy = context.Ado.GetDataTable("SELECT * FROM yx_studyinfo WHERE ID = 'b2bae47ddaed40f3972ebd03d99741fa'");
            dtStudy.TableName = "YX_StudyInfo";
            DataTable dtPatient = context.Ado.GetDataTable("SELECT * FROM YW_PatientVisitInfo WHERE ID = '89f20fb71d2b42758c7d8b16d1b2fbd0'");
            dtPatient.TableName = "YW_PatientVisitInfo";
            DataTable dtReport = context.Ado.GetDataTable("SELECT * FROM yw_reportinfo WHERE YX_StudyInfoID = 'b2bae47ddaed40f3972ebd03d99741fa'");
            dtReport.TableName = "YW_ReportInfo";
            DataSet set = new DataSet();
            set.Tables.Add(dtStudy.Copy());
            set.Tables.Add(dtPatient.Copy());
            set.Tables.Add(dtReport.Copy());

            Report report = new Report();
            report.Load(@"Templates\默认影像报告单2.frx");
            report.RegisterData(set);
            report.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Report report = new Report();
            report.Load(@"Templates\影像普通01.frx");

            report.SetParameterValue("Hospital", "安庆人民医院");
            report.SetParameterValue("CheckType", "DR影像");
            report.SetParameterValue("Witness", "Micheal");
            report.SetParameterValue("Time", DateTime.Now.ToShortTimeString());

            DRModel model = new DRModel
            {
                Insight = "",
                IMG = new Bitmap(@".\Resources\res.png"),
                Hospital = "XX市人民医院"
            };
            var dataset = TypeConverters.ToDataTable(new List<DRModel>() { model });
            
            report.RegisterData(dataset, "DataTable");
            report.Show(true);
        }

        public static byte[] Bitmap2Byte(Bitmap bitmap)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Jpeg);
                byte[] data = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(data, 0, Convert.ToInt32(stream.Length));
                return data;
            }
        }

        
    }
}
